package com.example.demo.config;

import com.example.demo.entity.sysuser.SysUser;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @author longzhonghua
 * @data 2018/11/04 22:30
 */

//@Configuration
//过时可以用WebMvcConfigurer
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //viewController控制器的名字，viewName：页面的名字（无需后缀名）
        registry.addViewController("/error/401").setViewName("/error/401");
        registry.addViewController("/error/403").setViewName("/error/403");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //和页面有关的静态目录都放在项目的static目录下 2019.1.5日打开了,因为后台访问不了这个位置
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //上传的图片在D盘下的OTA目录下，访问路径如：http://localhost:8081/OTA/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
        //其中OTA表示访问的前缀。"file:D:/OTA/"是文件真实的存储路径
        registry.addResourceHandler("/UPLOAD/**").addResourceLocations("file:F:/UPLOAD/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                HttpSession session = request.getSession();
                SysUser adminUser = (SysUser) session.getAttribute("sysUser");
                if(adminUser==null){
                    response.sendRedirect(request.getContextPath() +"/error/401");
                    return false;
                }
                //判断是否有权访问controller
                String uri = request.getRequestURI();
                if(!adminUser.getPermissions().contains(uri)) {
                    System.out.println("无权访问：" + uri);
                    System.out.println("权限列表：" + adminUser.getPermissions());
                    response.sendRedirect(request.getContextPath() +"/error/401");
                    return false;
                }
                return true;
            }
        }).excludePathPatterns("/admin/index","/admin","/admin/login","/admin/logout")
        .addPathPatterns("/admin/**");*/
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                HttpSession session = request.getSession();
                SysUser adminUser = (SysUser)session.getAttribute("sysUser");
                if(adminUser == null) {
                    response.sendRedirect(request.getContextPath() +"/error/401");
                    return false;
                }
                //检查Url是否在用户拥有的权限集合里面
                String url = request.getRequestURI();
                if(!adminUser.getPermissions().contains(url)) {
                    System.out.println("无权访问：" + url);
                    System.out.println("权限列表：" + adminUser.getPermissions());
                    response.sendRedirect(request.getContextPath() +"/error/403");
                    return false;
                }
                return true;
            }
        })
        .excludePathPatterns("/admin/index","/admin/login","/admin/logout","/admin")//不需要拦截的网址
        .addPathPatterns("/admin/**");//除了上面的网址外/admin开头的都需要被拦截;
    }
}
